package com.jcl.elasticsearch.api.controller;

import com.jcl.core.resp.ApiResponse;
import com.jcl.core.model.RedisChannelAccount;
import com.jcl.elasticsearch.core.entity.ResumeDownloadInfo;
import com.jcl.elasticsearch.core.thread.ResumeCrawlerContext;
import com.jcl.elasticsearch.core.utils.RedisCacheUtils;
import com.jcl.elasticsearch.recruit.service.RecruitElasticService;
import com.jcl.elasticsearch.resume.model.SearchResumeBasicInfo;
import com.jcl.elasticsearch.resume.service.ResumeCrawlerService;
import com.jcl.elasticsearch.resume.vo.ResumeCrawlerExportParam;
import com.jcl.elasticsearch.thread.ResumeIntoElasticTask;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * @ClassName ProducerController
 * @Description TODO
 * @Author yyliu
 * @Date 2018/11/12 17:00
 * @Version 1.0
 **/
@RestController
@RequestMapping("/elastic/crawler")
public class CrawlerController {

    @Autowired
    private ResumeCrawlerService resumeCrawlerService;

    @Autowired
    private RecruitElasticService recruitElasticService;

    /* *
     * @Author yyliu
     * @Description //TODO
     * @Date 10:47 2018/11/13
     * @Param [name]
     * @return com.jcl.core.resp.ApiResponse
     **/
    @RequestMapping(value = "/resume/export", method = RequestMethod.POST)
    public ApiResponse index(@RequestBody ResumeCrawlerExportParam resume) {
        //
        Map<String, String> validate = resumeCrawlerService.validateSearchResume(resume);

        SearchResumeBasicInfo searchResume = resumeCrawlerService.exportSearchResume(resume);

        ResumeCrawlerContext context = new ResumeCrawlerContext(searchResume.getId());

        // 开启线程
        ResumeIntoElasticTask.startResumeIntoElasticThread(context);

        return ApiResponse.success("OK");
    }

    /* *
     * @Author yyliu
     * @Description //TODO 
     * @Date 11:16 2018/11/13
     * @Param [corpCode, source, curPage, pageSize]
     * @return com.jcl.core.resp.ApiResponse
     **/
    @RequestMapping(value = "/corp/account", method = {RequestMethod.GET})
    public ApiResponse saveResume(String source){

        if(StringUtils.isEmpty(source)){
            return ApiResponse.error(" param 'source' is lose!");
        }

        String corpCode = (String) RedisCacheUtils.getChanelAccountCorpCode(source);
        RedisCacheUtils.addChanelAccountCorpCode(source, corpCode);
        RedisChannelAccount account = RedisCacheUtils.getChanelAccount(source, corpCode);

        return ApiResponse.success(account);
    }

    /** 
     * @Author yyliu
     * @Description //TODO 
     * @Date 11:16 2018/11/13
     * @Param [entity]
     * @return com.jcl.core.resp.ApiResponse
     **/
    @RequestMapping(value = "/resume/complete", method = {RequestMethod.GET, RequestMethod.POST})
    public ApiResponse completeResume(@RequestBody ResumeDownloadInfo entity){

        if(com.jcl.core.utils.StringUtils.isEmpty(entity.getTenantId())){
            return ApiResponse.error("param 'tenantId' is lose !");
        }
        recruitElasticService.completeResume(entity);

        return ApiResponse.success("SUCCESS");
    }


    /**
     * @Author yyliu
     * @Description //TODO
     * @Date 11:16 2018/11/13
     * @Param [entity]
     * @return com.jcl.core.resp.ApiResponse
     **/
    @RequestMapping(value = "/resume/keyword", method = RequestMethod.GET)
    public ApiResponse getKeyword(){
        Map<String,Object> map = new HashMap<>();

        String keys = "\"python\", \"爬虫\", \"运营\", \"客服\", \"市场\", \"销售\", \"行政\", \"HR\", \"go\", " +
                "\"spring\", \"vue\", \"react\", \"前端\", \"android\", \"ios\"";
        String[] kyeslist = keys.replaceAll("\"","").split(",");
        Random random = new Random();
        String keyword = kyeslist[random.nextInt(14)].trim();
        map.put("corpCode","JCL0000001");
        map.put("keywords",keyword);

        return ApiResponse.success(map);
    }


}
